package sg.edu.nus.iss.vmcs.entity.simuator;

import java.io.FileNotFoundException;
import java.io.IOException;

import sg.edu.nus.iss.vmcs.entity.Brand;
import sg.edu.nus.iss.vmcs.entity.BrandDrink;
import sg.edu.nus.iss.vmcs.entity.BrandDrinkStoreItem;
import sg.edu.nus.iss.vmcs.entity.BrandSnacks;
import sg.edu.nus.iss.vmcs.entity.BrandSnacksStore;
import sg.edu.nus.iss.vmcs.entity.BrandSnacksStoreItem;
import sg.edu.nus.iss.vmcs.entity.Coin;
import sg.edu.nus.iss.vmcs.entity.StoreItem;
import sg.edu.nus.iss.vmcs.entity.simuator.dao.ISimulatorDrinkBrandDAO;
import sg.edu.nus.iss.vmcs.entity.simuator.dao.ISimulatorSnacksBrandDAO;
import sg.edu.nus.iss.vmcs.entity.simuator.dao.impl.SimulatorSnacksBrandDAOImpl;

public class SimulatorSnacksBrandStore extends BrandSnacksStore {

	private static SimulatorSnacksBrandStore simulatorSnacksBrandStore = null;
	private String SNACKS_PROPERTIES = "source/SnacksPropertyFile.txt";
	private ISimulatorSnacksBrandDAO iSimulatorSnacksBrandDAO = null;
	
	String nameAttri = "Name";
	String priceAttri = "Price";
	String quantityAttri = "Quantity";
	
	private SimulatorSnacksBrandStore(){
		try {
			iSimulatorSnacksBrandDAO = SimulatorSnacksBrandDAOImpl.getSimulatorSnacksBrandDao(SNACKS_PROPERTIES);
			processStore();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void processStore() {
		int noOfItems = iSimulatorSnacksBrandDAO.getNumOfItems();
		this.setStoreSize(noOfItems);

		for(int i=1;i<=noOfItems;i++){
			try{
				String name = iSimulatorSnacksBrandDAO.getAttributeValue(nameAttri+i);
				String priceStr = iSimulatorSnacksBrandDAO.getAttributeValue(priceAttri+i);
				String quantityStr = iSimulatorSnacksBrandDAO.getAttributeValue(quantityAttri+i);
				double price = Double.parseDouble(priceStr);
				int quantity = Integer.parseInt(quantityStr);
				BrandSnacks storeObject = new BrandSnacks(name, ""+i,price);
				StoreItem storeItem = new BrandSnacksStoreItem(storeObject,quantity);
				this.addItem((i-1), storeItem);
			}catch(Exception e){
				
			}
		}
	}

	public static BrandSnacksStore getInstance() {
		if(simulatorSnacksBrandStore == null)
			simulatorSnacksBrandStore = new SimulatorSnacksBrandStore();
		return simulatorSnacksBrandStore;
	}
	@Override
	public void storeRecords(){
		for (int i = 0; i < size; i++) {
			if (items[i] == null || items[i].getContent() == null)
				continue;
			StoreItem storeItem = items[i];
			Brand coin = (Brand) storeItem.getContent();
			int quantity = storeItem.getQuantity();
			String id = coin.getId();
			
			String name = coin.getName();
			String price = ""+coin.getPrice();
			try {
				iSimulatorSnacksBrandDAO.update(nameAttri+id,name);
				iSimulatorSnacksBrandDAO.update(priceAttri+id,price);
				iSimulatorSnacksBrandDAO.update(quantityAttri+id,""+quantity);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}            
		}
	}
	
	public void updateItem(StoreItem object){
		super.updateItem(object);
		storeRecords();
	}

}
